package com.yenlien.traceyun.breed.service;

import com.yenlien.traceyun.breed.dao.BreedDao;
import com.yenlien.traceyun.breed.dao.QuarantineDao;
import com.yenlien.traceyun.breed.entity.Breed;
import com.yenlien.traceyun.breed.entity.BreedInputs;
import com.yenlien.traceyun.breed.entity.Quarantine;
import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/9/25.
 */
@Service
public class QuarantineService {
    @Autowired
    private QuarantineDao quarantineDao;
    @Autowired
    private BreedDao breedDao;
    @Autowired
    private FileRestService fileRestService;

    public Page<Quarantine> findByList(final Quarantine quarantine) {
        Pageable pageable = PageRequest.of(quarantine.getPage(), quarantine.getPageSize(), Sort.Direction.DESC, "createtime");
        return quarantineDao.findAll(new Specification<Quarantine>() {
            @Override
            public Predicate toPredicate(Root<Quarantine> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //构造查询条件
                List<Predicate> list_and = new ArrayList<>();
                List<Predicate> list_or = new ArrayList<>();
                if (!StringUtils.isEmpty(quarantine.getCompanyId())) {
                    list_and.add(cb.equal(root.get("companyId"), quarantine.getCompanyId()));
                }
                Predicate[] predicate = new Predicate[list_and.size()];
                Predicate pre_and = cb.and(list_and.toArray(predicate));
                if (!StringUtils.isEmpty(quarantine.getQuery())) {
                    list_or.add(cb.like(root.get("breedBatch"), "%" + quarantine.getQuery() + "%"));
                    list_or.add(cb.like(root.get("breedName"), "%" + quarantine.getQuery() + "%"));
                } else {
                    return pre_and;
                }
                Predicate[] or = new Predicate[list_or.size()];
                Predicate pre_or = cb.or(list_or.toArray(or));
                return query.where(pre_and, pre_or).getRestriction();
            }
        }, pageable);
    }

    @Transactional
    public void save(Quarantine quarantine, List<MultipartFile> files) throws Exception {
        if (files.size() > 0) {
            if (StringUtils.isEmpty(quarantine.getId())) {
                fileRestService.upload(null, quarantine, files);
            } else {
                Quarantine old = quarantineDao.findById(quarantine.getId());
                fileRestService.upload(old, quarantine, files);
            }
        }
        quarantine.setLastStep(quarantine.getBreedId());
        quarantineDao.save(quarantine);
        Breed breed = breedDao.findById(quarantine.getBreedId());
        breed.setIsQuarantine(1);
        breedDao.save(breed);
    }

    public Breed findById(String id) {
        Breed breed = breedDao.findById(id);
        breed.setQuarantine(quarantineDao.findByBreedId(id));
        return breed;
    }
}
